package com.demo.bookstore.service.impl;

import com.demo.bookstore.mapper.BookEntityManualMapper;
import com.demo.bookstore.model.BookEntity;
import com.demo.bookstore.service.BookService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.List;
import java.util.stream.Collectors;

@Service
@Slf4j
public class BookServiceImpl implements BookService {

    private BookEntityManualMapper bookEntityManualMapper;

    @Autowired
    public void setBookEntityManualMapper(BookEntityManualMapper bookEntityManualMapper) {
        this.bookEntityManualMapper = bookEntityManualMapper;
    }
    @Override
    public List<BookEntity> queryBooks(String name) {
        return bookEntityManualMapper.queryByName(name);
    }

    @Override
    public Boolean addBook(BookEntity book) {
        return bookEntityManualMapper.insertSelective(book) == 1 ? true : false;
    }

    @Override
    public List<BookEntity> queryAllByPage(int pageBegin, int pageSize) {
        return bookEntityManualMapper.selectByPage(pageBegin, pageSize);
    }

    @Override
    public Boolean deleteById(int id) {
        return 1 == bookEntityManualMapper.deleteByPrimaryKey(id);
    }

    @Override
    public int batchInsert(List<BookEntity> bookEntityList) {
        if (bookEntityList == null || bookEntityList.size() == 0) {
            return 0;
        }
        int totalSize = 0;
        int batchSize = 1000;
        int start = 0;
        while (start < bookEntityList.size()) {
            List<BookEntity> subList = bookEntityList.subList(start, Math.min(start + batchSize, bookEntityList.size()));
            start += batchSize;
            try {
                totalSize += bookEntityManualMapper.batchInsert(subList);
            } catch (Exception e) {
                for (BookEntity book : subList) {
                    try {
                        totalSize += bookEntityManualMapper.insertSelective(book);
                    } catch (Exception ex) {
                        log.error("data insert error",ex);
                    }
                }
            }
        }
        return totalSize;
    }

    @Override
    public Boolean updateBook(BookEntity book) {
        return 1 == bookEntityManualMapper.updateByPrimaryKeySelective(book);
    }

    @Override
    public String export(BookEntity book) {
        List<BookEntity> books = bookEntityManualMapper.queryByName(book.getName());
        if (CollectionUtils.isEmpty(books)) {
            return null;
        }
        return books.stream().map(this::formatBook).collect(Collectors.joining("\n"));
    }

    private String formatBook(BookEntity book) {
        return String.format(
                "%d|%s|%s|%s|%s",
                book.getId(),
                book.getName(),
                book.getClassification(),
                book.getDescription(),
                book.getPublishDate()
        );
    }
}
